package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.Salesdetail;
import com.example.buildtable.service.SalesdetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "销售明细报")
@RequestMapping(value = "/salesdetail/")
public class SalesdetailController {
    @Autowired
    private SalesdetailService salesdetailService;
    
    @Operation(summary = "查询所有销售明细报数据")
    @GetMapping(value = "/getAllSalesdetail")
    public R getAllSalesdetail() {
        List<Salesdetail> salesdetailList = salesdetailService.list();
        return R.ok("查询所有销售明细报数据成功").data("salesdetailList", salesdetailList);
    }
    
    @Operation(summary = "增加销售明细报")
    @PostMapping(value = "/addSalesdetail")
    public R addSalesdetail(Salesdetail salesdetail) {
        boolean flag = salesdetailService.save(salesdetail);
        if (flag) {
            return R.ok("增加销售明细报成功");
        } else {
            return R.ok("增加销售明细报失败");
        }
    }

    @Operation(summary = "删除销售明细报")
    @GetMapping(value = "/deleteSalesdetail/{salesDetailId}")
    public R deleteSalesdetail(@PathVariable("salesDetailId") Integer salesDetailId) {
        boolean flag = salesdetailService.removeById(salesDetailId);
        if (flag) {
            return R.ok("删除销售明细报成功");
        } else {
            return R.ok("删除销售明细报失败");
        }
    }

    @Operation(summary = "按照销售明细报查看")
    @GetMapping(value = "/getSalesdetailById/{salesDetailId}")
    public R getSalesdetailById(@PathVariable("salesDetailId") Integer salesDetailId) {
        Salesdetail salesdetail = salesdetailService.getById(salesDetailId);
        if (salesdetail != null) {
            return R.ok("按照销售明细报查看成功").data("salesdetail", salesdetail);
        } else {
            return R.error("按照销售明细报查看失败");
        }
    }

    @Operation(summary = "修改销售明细报")
    @PostMapping(value = "/updateSalesdetail")
    public R updateSalesdetail(Salesdetail salesdetail) {
        boolean flag = salesdetailService.updateById(salesdetail);
        if (flag) {
            return R.ok("修改销售明细报成功");
        } else {
            return R.ok("修改销售明细报失败");
        }
    }
}